package robotleMvc.ui.lizhi.zhuanQuan
{
	import _Pan3D.core.MathTween;
	import _Pan3D.display3D.Display3DSprite;
	
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.Program3D;
	import flash.display3D.VertexBuffer3D;
	import flash.geom.Matrix3D;
	
	public class ZhuanQuanDispay3DSprite extends Display3DSprite
	{
		protected var lizhiVertexBuffer:VertexBuffer3D;
		protected var lizhiStateVertexBuffer:VertexBuffer3D;
		protected var lizhiIndexBuffer:IndexBuffer3D;
		protected var _program3D:Program3D;
		protected var _timer:Number=0;
		public function ZhuanQuanDispay3DSprite(context:Context3D)
		{
			super(context);
		}
		override public function update() : void {
			_context.setBlendFactors(Context3DBlendFactor.ONE,Context3DBlendFactor.ONE);
			if (!this._visible||_timer>=50) {
				_timer=0
				return;
			}
			if (_objData && _objData.texture) {
				if(_program3D){
					_context.setProgram(_program3D);
				}else{
					_context.setProgram(this.program);
				}
				setVc();
				setVa();
				resetVa();
			}
		}

		override protected function uplodToGpu() : void {

			
			var vv:Vector.<Number>=new Vector.<Number>;
			var uu:Vector.<Number>=new Vector.<Number>;
			var ss:Vector.<Number>=new Vector.<Number>; //要确定每组成几个数据;暂为3跟顶点一样
			var ii:Vector.<uint>=new Vector.<uint>;
			var inum:int=_objData.vertices.length/3;
			var zAngle:Number=0;
			var yAngle:Number=0;
			var angle:int = 0;
			for(var i:int;i<5;i++){
				vv=vv.concat(_objData.vertices);
				uu=uu.concat(_objData.uvs);
				zAngle = angle*Math.PI/180;
				yAngle=Math.random()*2*Math.PI
				for(var k:int=0;k<inum;k++){
					ss.push(0,yAngle,zAngle);// 倾斜
				}
				for(var j:int=0;j<_objData.indexs.length;j++){
					ii.push(_objData.indexs[j]+inum*i);
				}
				angle += 30;
				
			}
			_objData.vertexBuffer = this._context.createVertexBuffer(vv.length/ 3, 3);
			_objData.vertexBuffer.uploadFromVector(Vector.<Number>(vv), 0, vv.length / 3);
			
			_stateBuffer = this._context.createVertexBuffer(ss.length/ 3, 3); //存状态;
			_stateBuffer.uploadFromVector(Vector.<Number>(ss), 0, ss.length / 3);
			
			_objData.uvBuffer = this._context.createVertexBuffer(uu.length / 2, 2);
			_objData.uvBuffer.uploadFromVector(Vector.<Number>(uu), 0, uu.length / 2);
			
			_objData.indexBuffer = this._context.createIndexBuffer(ii.length);
			_objData.indexBuffer.uploadFromVector(Vector.<uint>(ii), 0, ii.length);

		}
		
		override protected function setVa() : void {
			
			//_timer++;
			_context.setVertexBufferAt(0, _objData.vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1, _objData.uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
			_context.setVertexBufferAt(2, _stateBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setTextureAt(0, _objData.texture);
			_context.drawTriangles(_objData.indexBuffer, 0, -1);
		}
		private var _stateBuffer:VertexBuffer3D;
		
		override protected function resetVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setVertexBufferAt(2, null);
			_context.setTextureAt(0,null);
		}
		override protected function setVc() : void {
			this.updateMatrix();
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			_timer++
			var t:Number=MathTween.easeInOut(_timer,0,360,50);
			var b:Number=t*Math.PI/180;
			var c:Number=0;
			var d:Number=0;
			_context.setProgramConstantsFromVector(Context3DProgramType.VERTEX,8,Vector.<Number>( [t,b,c,d])); //这里暂可以传一个时间进去;
		
			var n:Number=(1-Math.abs(180-t)/180)
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,0,Vector.<Number>( [n,1,1,1]));
			
			
		}
		public function get program3D():Program3D
		{
			return _program3D;
		}
		public function set program3D(value:Program3D):void
		{
			_program3D = value;
		}
		public function set play(temp:Boolean):void{
			_timer=temp?0:99;
			
		}
		
	}
}